package com.zhida84.Servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.zhida84.Convert.StringUtil;
import com.zhida84.Dao.RecruitDao;
import com.zhida84.Impl.RecruitImpl;

public class sendMsgInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// 设置返回的数据编码格式
		response.setContentType("text/html;charset=GBK");
		// 设置发送数据的编码格式
		request.setCharacterEncoding("UTF-8");
		try {
			// 返回数据对象
			PrintWriter out = response.getWriter();
			// 获取登录成功后插入的常规数据，如用户id和用户手机号码
			Cookie[] cookies = request.getCookies();
			Integer userId = null;
			if (cookies != null) {
				// 循环cookies里面的数据
				for (Cookie c : cookies) {
					// 找到key为phone的数据，返回
					if (c.getName().equals("userId"))
					{
						userId = Integer.valueOf(c.getValue());
						break;
					}
				}
			}
			// 根据key获取具体的数据值
			String msg =  request.getParameter("msg");
			Integer recruitId = Integer.valueOf(request.getParameter("recruitId"));
			// 判断当前参数是否全为空
			if (StringUtil.isEmpty(userId)) {
				out.print("请登录!");
				return;
			}
			if (!StringUtil.isEmpty(recruitId, msg)) {
				// 实际业务逻辑
				RecruitDao recruitDao = new RecruitImpl();
				int result = recruitDao.sendMsgInfo(userId, recruitId, msg);
				if (result > 0) {
					out.print("发送成功");
				} else {
					out.print("发送失败");
				}
			} else {
				out.print("缺少必要的参数!");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
